﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace AcademicPlanner.Framework.Libraries.Mvc
{
    public class LocalizedSkinnedViewEngine : WebFormViewEngine
    {
		private string skinName;

		private string localeName;

		public LocalizedSkinnedViewEngine(string pSkin, string pLocale)
		{
			skinName = pSkin;
			localeName = pLocale;
			
			initFormats();
		}

		private void initFormats()
		{
			ViewLocationFormats = new[] {
				"~/Views/Skins/" + skinName + "/{1}/" + localeName + "/{0}.aspx",
				"~/Views/Skins/" + skinName + "/Shared/" + localeName + "/{0}.ascx",

				"~/Views/Skins/" + skinName + "/" + localeName + "/{1}/{0}.aspx",
				"~/Views/Skins/" + skinName + "/" + localeName + "/Shared/{0}.ascx",

				"~/Views/Skins/" + skinName + "/{1}/{0}.aspx",
                "~/Views/Skins/" + skinName + "/Shared/{0}.ascx",
				
				"~/Views/Skins/" + skinName + "/" + localeName + "/{0}.aspx",
				"~/Views/Skins/" + skinName + "/" + localeName + "/{0}.ascx",

				"~/Views/Skins/" + skinName + "/{0}.aspx",
                "~/Views/Skins/" + skinName + "/{0}.ascx",

				"~/Views/{1}/" + localeName + "/{0}.aspx",
				"~/Views/Shared/" + localeName + "/{0}.ascx",

				"~/Views/" + localeName + "/{1}/{0}.aspx",
				"~/Views/" + localeName + "/Shared/{0}.ascx",

                "~/Views/{1}/{0}.aspx",
                "~/Views/Shared/{0}.ascx",

				"~/" + localeName + "/{0}.aspx",
                "~/" + localeName + "/{0}.ascx",

				"~/{0}.aspx",
                "~/{0}.ascx",
            };

			MasterLocationFormats = new[] {
				"~/Views/Skins/" + skinName + "/{1}/" + localeName + "/{0}.Master",
				"~/Views/Skins/" + skinName + "/" + localeName + "/{1}/{0}.Master",
				"~/Views/Skins/" + skinName + "/{1}/{0}.Master",

				"~/Views/Skins/" + skinName + "/Shared/" + localeName + "/{0}.Master",
				"~/Views/Skins/" + skinName + "/Shared/{0}.Master",

				"~/Views/Skins/" + skinName + "/" + localeName + "/{0}.Master",
				"~/Views/Skins/" + skinName + "/{0}.Master",

				"~/Views/{1}/" + localeName + "/{0}.Master",
				"~/Views/" + localeName + "/{1}/{0}.Master",
                "~/Views/{1}/{0}.Master",
				
				"~/Views/Shared/" + localeName + "/{0}.Master",
                "~/Views/Shared/{0}.Master",
				
				"~/" + localeName + "/{0}.Master",
				"~/{0}.Master"
            };

			PartialViewLocationFormats = ViewLocationFormats;
		}

		protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
		{
			return base.VirtualPathProvider.FileExists(virtualPath);
		}
    }
}
